require "lib/timus_access"
require "lib/uva_access"
class RunsController < ApplicationController
  layout 'application'
  before_filter :login_required, :only => [ :new, :create ]
  before_filter :store_location
  def index
    @selected_runs = Run.paginate :page => params[:page], :order => 'created_at DESC', :per_page => 20
  end

  def new
    @problem_id = params[:num]
  end

  def show
    if(params[:id].nil?)
      redirect_to :action => "index"  
    end
    @run = Run.find_by_id(params[:id])
  end

  def create
    @Programming_languages = {"1" => :C, "2" => :CPP, "3" => :Paskal, "4" => :CSharp, "5" => :Java}
    language = @Programming_languages[params[:problem][:language]]
    problem = Problem.find(params[:problem][:id])
    identifier = problem.source.name.constantize::submit_problem(params[:problem][:source], 
      problem, 
      language, current_user)
    if(!identifier.nil?)
      new_run = Run.new
      new_run.problem = problem 
      new_run.status =  nil
      new_run.realid = identifier
      new_run.user = current_user
      new_run.language = language.to_s
      new_run.save
      flash[:notice] = "Submit successful!!"
      redirect_to :action => "index"
    else
      flash[:error] = "There was a problem with your submit"
      redirect_to :action => "new", :num => problem
    end
  end

  def destroy
  end

  def edit
    redirect_to :action => "index"
  end

end
